package com.mi.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import com.mi.common.core.util.Result;
import com.mi.common.log.annotation.Log;
import com.mi.system.dto.query.DeptQueryDTO;
import com.mi.system.dto.request.DeptRequestDTO;
import com.mi.system.entity.Dept;
import com.mi.system.service.DeptService;
import com.mi.system.vo.DeptTreeVO;
import com.mi.system.vo.DeptVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

import static com.mi.common.core.util.Result.ok;

/**
 * Dept controller
 *
 * @author jin_huaquan
 * @version 1.0
 * @date 2021/11/1 14:53
 */
@Api(value = "部门API", tags = "Dept")
@ApiSort(996)
@RestController
@RequestMapping("/api/v1/depts")
@RequiredArgsConstructor
@Slf4j
public class DeptController {

    private final DeptService deptService;

    @ApiOperation(value = "分页查询部门列表", notes = "分页查询部门列表")
    @ApiOperationSupport(author = "jin_huaquan@finsung.com", order = 1)
    @ApiImplicitParam(name = "name", value = "部门的名称", paramType = "query", dataTypeClass = String.class)
    @Log(title = "查询部门列表")
    @GetMapping("/actions/list")
    public Result<IPage<DeptVO>> listDept(Page page, DeptQueryDTO query) {
        log.info("API CALL : list dept.");

        return ok(deptService.listDept(page, query));
    }

    @ApiOperation(value = "查询部门列表", notes = "查询部门列表")
    @ApiOperationSupport(author = "jin_huaquan@finsung.com", order = 2)
    @ApiImplicitParam(name = "name", value = "部门的名称", paramType = "query", dataTypeClass = String.class)
    @Log(title = "罗列部门")
    @GetMapping("/actions/query")
    public Result<List<DeptVO>> listDept(DeptQueryDTO query) {
        log.debug("API CALL : query dept.");

        return ok(deptService.listDept(query));
    }

    @ApiOperation(value = "查询部门树", notes = "查询部门树")
    @ApiOperationSupport(author = "jin_huaquan@finsung.com", order = 3)
    @ApiImplicitParam(name = "parentId", value = "上级部门ID", paramType = "path", dataType = "java.lang.String")
    @Log(title = "查询部门组织结构")
    @GetMapping("/{parentId}/trees")
    public Result<List<DeptTreeVO>> listDeptTree(@PathVariable Long parentId) {
        log.debug("API CALL : list dept tree.");

        return ok(deptService.listDeptTree(parentId));
    }

    @ApiOperation(value = "保存部门", notes = "保存部门")
    @ApiOperationSupport(author = "jin_huaquan@finsung.com", order = 4)
    @Log(title = "新增部门:#{#request.name}")
    @PostMapping("")
    public Result<Boolean> saveDept(@Valid @RequestBody DeptRequestDTO request) {
        log.debug("API CALL : save dept.");

        return ok(deptService.saveDept(request));
    }

    @ApiOperation(value = "更新部门", notes = "更新部门信息")
    @ApiOperationSupport(author = "jin_huaquan@finsung.com", order = 5)
    @ApiImplicitParam(name = "id", value = "部门的ID", paramType = "path", dataType = "java.lang.String", required = true)
    @Log(title = "编辑部门:#{#request.name}")
    @PutMapping("/{id}")
    public Result<Boolean> updateDept(@PathVariable Long id, @Valid @RequestBody DeptRequestDTO request) {
        log.debug("API CALL : update dept.");

        return ok(deptService.updateDept(id, request));
    }

    @ApiOperation(value = "删除部门", notes = "删除部门")
    @ApiOperationSupport(author = "jin_huaquan@finsung.com", order = 6)
    @ApiImplicitParam(name = "id", value = "部门的ID", paramType = "path", dataType = "java.lang.String", required = true)
    @Log(title = "删除部门:#{#request.name}")
    @DeleteMapping("/{id}")
    public Result<Dept> removeDept(@PathVariable Long id) {
        log.debug("API CALL : remove dept.");

        return ok(deptService.removeDept(id));
    }
}
